package com.renwen.sms.controller;


import com.renwen.sms.dto.CourseDto;
import com.renwen.sms.dto.PageResult;
import com.renwen.sms.entity.Course;
import com.renwen.sms.entity.SelectTeacher;
import com.renwen.sms.exception.MyRuntimeException;
import com.renwen.sms.service.CourseService;
import com.renwen.sms.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * 课程信息控制层
 */
@RestController
@CrossOrigin
@RequestMapping("course/")
public class CourseController {
    @Resource
    CourseService courseService;

    /**
     * 查询所有课程信息
     * @param page 页
     * @param size 尺寸
     * @param formMap 表格信息
     * @return course集合
     */
    @PostMapping("list")
    public PageResult<CourseDto> list(Integer page, Integer size, @RequestBody Map<String, Object> formMap) {
        try {
            return courseService.list(page, size, formMap);
        } catch (Exception e) {
            throw new MyRuntimeException("目前还没有数据。");
        }
    }


    /**
     * 根据课程id编号查询信息
     *
     * @param id 编号
     * @return 课程信息
     */
    @GetMapping("/getById")
    public Result get(Integer id) {
        try {
            return courseService.get(id);
        } catch (Exception e) {
            throw new MyRuntimeException("系统错误，嘻嘻！");
        }
    }

    /**
     * 添加课程
     *
     * @param course 课程对象
     * @return 添加结果
     */
    @PostMapping("/add")
    public Result add(@RequestBody Course course) {
        try {
            courseService.save(course);
            return new Result("添加成功！", course.getCNo());
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 修改课程信息
     *
     * @param course 课程对象
     * @return 操作提示信息
     */
    @PostMapping("/update")
    public Result update(@RequestBody Course course) {
        try {
           courseService.update(course);
            return new Result("修改成功！", course.getCNo());
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 删除课程
     *
     * @param cNo 课程号
     * @return 操作提示信息
     */
    @GetMapping("/delete")
    public Result del(String cNo) {
        try {
            courseService.delete(cNo);
            return new Result("删除成功！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 根据课程编号组批量删除
     *
     * @param ids
     * @return
     */
    @GetMapping("/batchDel")
    public Result batchDel(String ids) {
        try {
            courseService.batchDelete(ids);
            return new Result("删除成功！");
        } catch (Exception e) {
            return new Result(400, e.getMessage());
        }
    }

    /**
     * 为未安排课程的指定教师
     */
    @PostMapping("/selectTeacher")
    public Result selectTeacher(@RequestBody SelectTeacher selectTeacher) {
        try {
            courseService.selectTeacher(selectTeacher);
            return new Result(200,"指定成功！");
        } catch (Exception e) {
            return new Result(400,e.getMessage());

        }
    }

}
